<template>
  <div class="login_container">
    <div class="login_box">
      <!-- avatar area -->
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- form area
                 note: Data of el-form need bind, is :model, not v-model!
      -->
      <el-form
        label-width="0"
        ref="loginForm_ref"
        :model="loginForm"
        :rules="loginFormRules"
        class="login_form"
      >
        <!-- username -->
        <el-form-item prop="username">
          <el-input
            suffix-icon="iconfont icon-user"
            v-model="loginForm.username"
            placeholder="Username"
          ></el-input>
        </el-form-item>
        <!-- password -->
        <el-form-item prop="password">
          <el-input
            type="password"
            suffix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            placeholder="Password"
          ></el-input>
        </el-form-item>
        <!-- button area -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">Login</el-button>
          <el-button type="info" @click="resetLoginForm">Reset</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data: () => {
    return {
      /* Data of login form bind */
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      /* Validation rules of login form */
      loginFormRules: {
        /* Is the username valid? */
        username: [
          {
            required: true,
            message: 'Please enter your username',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 13,
            message: 'length is 3 to 13 characters',
            trigger: 'blur'
          }
        ],
        /* Is the password valid? */
        password: [
          {
            required: true,
            message: 'Please enter your password',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 19,
            message: 'length is 6 to 19 characters',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    /* Reset login Form  */
    resetLoginForm: function () {
      this.$refs.loginForm_ref.resetFields()
    },
    /* Validate form data and login */
    login: function () {
      this.$refs.loginForm_ref.validate(async valid => {
        /* Initiate a login request */
        if (!valid) return
        const rs = await this.$http.post('login', this.loginForm)
        if (rs.meta.status !== 200) {
          return this.$msg.error(rs.meta.msg)
        }
        this.$message.success('Login success!')
        /* 1. save token */
        window.sessionStorage.setItem('token', rs.data.token)
        /* 2. go home, router redirect '/', ok! */
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #ffffff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  /* 嵌套-头像框 */
  .avatar_box {
    height: 129px;
    width: 129px;
    border: 1px solid #eeeeee;
    border-radius: 50%;
    padding: 9px;
    box-shadow: 0 0 9px #dddddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eeeeee;
    }
  }
}
.login_form {
  position: absolute;
  width: 100%;
  bottom: 0%;
  padding: 0 21px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
